var express = require('express')
var router = express.Router()
const connection = require('./mysql')

connection.connect(err => {
  if (err == null) {
    console.log('数据库链接成功')
  }
})


/**接收checklogin 登录请求 */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' })
  // console.log(req);
  console.log('连接成功')
})


router.post('/checklogin', (req, res) => {
  let { username, password } = req.body
  
  var sqlStr = `select * from users where username = '${username}' and password = '${password}'`
  connection.query(sqlStr, (err, data) => {
    console.log(err);
    
    if (err !== null) {
      res.send(
        JSON.stringify({
          code: false,
          msg: '登陆失败'
        })
      )
      return
    } else {
      res.send(
        JSON.stringify({
          code: true,
          msg: '登陆成功',
          result: data[0]
        })
      )
      return
    }
  })
})

module.exports = router
